"use strict";

import * as DicDemo from './dicDemo.js'
import * as RandomDemo from './randomDemo.js'

//*
export class DemoSet {
    static constructCount=0; // 记录 构造函数 被调用的次数
    constructor() {
        this.items = {}
        DemoSet.constructCount += 1;
        this.constructCount = DemoSet.constructCount;
    }

    has(element) {
        return element in this.items
        // Object 原型有 hasOwnProperty 方法。该方法返回一个表明对象是否具有特定属性的布尔值
        // return Object.prototype.hasOwnProperty.call(this.items, element)
    }

    add(element) {
        if (!this.has(element)) {
            this.items[element] = element
            return true
        }
        return false
    }

    delete(element) {
        if (this.has(element)) {
            delete this.items[element]
            return true
        }
        return false
    }

    clear() {
        this.items = {}
    }

    size() {
        // JavaScript 的 Object 类有一个 keys 方法，它返回一个包含给定对象所有属性的数组
        return Object.keys(this.items).length
    }

    values() {
        // Object.values()方法返回了一个包含给定对象所有属性值的数组
        return Object.values(this.items)
    }

    getConstructCount() {
        return this.constructCount;
    }

    deepClone() {
        let newObj = new DemoSet();
        newObj.items = DicDemo.deepClone(this.items);
        return newObj;
    }
}
//*/

//const genRandom = (min, max) => (Math.random() * (max - min + 1) | 0) + min;

function setLoop() {
    let  setObj = new DemoSet();
    for(var i=0; i<100; i+=1) {
        let rNum = RandomDemo.genIntRandom(0,100);
        setObj.add(rNum);
    }

    console.log("添加100个个随机数以后, 集合 size="+setObj.size());
    setObj.values().forEach((element, index) => {
        console.log("集合遍历, index = "+index+", element = "+element);
    });

    let deepCloneObj = setObj.deepClone();
    deepCloneObj.add(1000);
    console.log("deepClone 后，原集合对象 size="+setObj.size());
    console.log("deepClone 后，克隆集合对象  size="+deepCloneObj.size());
}

function setSort() {
    let  setObj = new DemoSet();
    for(var i=0; i<100; i+=1) {
        //let rNum = RandomDemo.genFloatRandomBetween(1,1.2);
        let rNum = RandomDemo.genIntRandom(0,100);
        setObj.add(rNum);
    }

    console.log("添加100个个随机数以后, 集合 size="+setObj.size());
    setObj.values().sort().forEach((element, index) => {
        console.log("排序后，集合遍历, index = "+index+", element = "+element);
    });
}

function demoLogic() {
    console.log("\nsetDemoLogic");
    setLoop();

    setSort();
}

//module.exports.deepClone = deepClone;
//module.exports.demoLogic = demoLogic;

export{demoLogic};
